package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.vo.TSystemAllgroup;
import com.tjsinfo.tjsoa.system.vo.TSystemPost;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUserallgroup;

public class SystemAllGroupBO implements ISystemAllGroupBO {
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	
	
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public Integer addSystemAllGroup(TSystemAllgroup systemAllgroup,
			List<TSystemUserallgroup> systemUserallgroupList) {
		// TODO Auto-generated method stub
		Integer groupId =  hibernateDao.save(systemAllgroup);
		for (TSystemUserallgroup systemUserallgroup : systemUserallgroupList) {
			systemAllgroup.setId(groupId);
			systemUserallgroup.setSystemAllgroup(systemAllgroup);
			hibernateDao.saveObject(systemUserallgroup);
		}
		return groupId;
	}

	public Boolean deleteSystemAllGroup(TSystemAllgroup systemAllgroup) {
		// TODO Auto-generated method stub
		try {
			systemAllgroup.setState(0);
			hibernateDao.update(systemAllgroup);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public List<TSystemAllgroup> selectAllGroup(Integer first, Integer max) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemAllgroup.class);
		List<Order> orderlist= new ArrayList<Order>();
		orderlist.add(Order.asc("orderNo"));
		return qbcDao.findPageByCriteria(criteria, orderlist, max, first).getItems();
		
	}

	public Boolean updateSystemAllGroup(TSystemAllgroup systemAllgroup) {
		// TODO Auto-generated method stub
		try {			
			hibernateDao.update(systemAllgroup);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public Integer selectTotalLine(){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TSystemAllgroup.class);
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}
	
	/**
	 * 组名称是否相同
	 * @param 组名称
	 * @return
	 */
	public Boolean isSameSystemAllGroupName(Integer id,String postname){
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemAllgroup.class);		
		criteria.add(Restrictions.eq("groupName", postname));
		criteria.add(Restrictions.not(Restrictions.eq("id", id)) );
		criteria.add(Restrictions.eq("state", 0));
		if (qbcDao.getCriteria(criteria).size() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 查询系统组
	 * @param id
	 * @return
	 */
	public TSystemAllgroup selectTSystemAllgroupById(Integer id){
		TSystemAllgroup systemAllgroup =  (TSystemAllgroup) hibernateDao.findById(TSystemAllgroup.class, id);
		return systemAllgroup;
	}
	
	/**
	 * 查询系统用户组
	 * @param id
	 * @return
	 */
	public List<TSystemUserallgroup> selectTSystemUserAllgroupById(Integer id){
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserallgroup.class);
		criteria.add(Restrictions.eq("systemAllgroup.id", id));
		List<TSystemUserallgroup> systemUserallgroupList = qbcDao.getCriteria(criteria);
		return systemUserallgroupList;
	}
	
	/**
	 * 删除系统用户组
	 * @param id
	 * @return
	 */
	public Boolean deleteTSystemUserAllgroupById(Integer id){
		List<TSystemUserallgroup> list = selectTSystemUserAllgroupById(id);
		for (TSystemUserallgroup systemUserallgroup : list) {
			hibernateDao.delete(systemUserallgroup);
		}
		
		return true;
	}
	
	/**
	 * 添加系统用户组
	 * @param id
	 * @return
	 */
	public Boolean addTSystemUserAllgroupById(TSystemUserallgroup systemUserallgroup){
		hibernateDao.saveObject(systemUserallgroup);
		return true;
	}
}
